<template>
  <div class="login-con">
    <el-card class="box-card">
      <div class="title-box">
        <img src="@/assets/img/logo.png" class="logo"/>
        <img src="@/assets/img/cn.png" class="lang"/>
      </div>
      <!-- 登录表单 -->
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="account">
          <div>{{ $t("lang.account") }}:</div>
          <el-input type="text" v-model="ruleForm.account" autocomplete="off">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <div>{{ $t("lang.password") }}:</div>
          <el-input type="password" v-model="ruleForm.password" autocomplete="off" show-password>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" class="submitbtn">{{ $t("lang.login") }}</el-button>
        </el-form-item>
      </el-form>
<!--      国际化设置-->
      <el-select v-model="lang" @change="changeLang" placeholder="$('lang.lang')">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      ruleForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [{required: true, trigger: "blur", message: "请输入用户名"}],
        password: [{required: true, trigger: "blur", message: "请输入密码"}],
      },
      options:[
        {
          value:"zh",
          label:this.$t("lang.cs"),
        },
        {
          value:"en",
          label: this.$t("lang.is"),
        },
      ],
      lang:"",
    };
  },
  methods: {
    //登录
    submitForm() {
      console.log("登录函数被调用了");
      //1.验证输入的用户名和密码是否正确
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //验证成功
          //调用vuex 中 user子树里面的 登录的 action动作
          this.$store.dispatch("LoginAction",this.ruleForm);
        } else {
          //验证没通过
          console.log("验证没通过");
        }
      });
    },
    //切换语言集
    changeLang(){
      console.log("切换语言集触发了");
      console.log(this.$i18n.locale);
      this.$store.commit("SET_LANG", this.lang);
      this.$router.go(0);
    },
  },
  created() {
    console.log(this);
    this.lang = this.$store.getters.locale;
  },
};
</script>

<style scoped lang="scss">
.login-con {
  .box-card {
    position: relative;
    width: 350px;
    margin: 200px auto;
    padding: 20px;

    .title-box {
      height: 80px;
      line-height: 80px;
      text-align: center;
    }

    .login {
      width: 140px;
    }

    .lang {
      position: absolute;
      top: 0;
      right: 0;
      width: 50px;
    }
    .logo{
      width: 50%;
    }
  }
  .submitbtn{
    width: 100%;
  }
}
</style>